{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 图像的读取与显示"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 图像读取"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 图像读取入口参数：\n",
    "  - cv2.IMREAD_COLOR：彩色图像\n",
    "  - cv2.IMREAD_GRAYSCALE：灰度图像"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T10:03:04.166093400Z",
     "start_time": "2024-04-25T10:03:02.927363200Z"
    }
   },
   "outputs": [],
   "source": [
    "import cv2  #opencv的缩写为cv2\n",
    "\n",
    "# 魔法指令，直接展示图，Jupyter notebook 特有\n",
    "%matplotlib inline   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-26T01:04:01.769476900Z",
     "start_time": "2024-04-26T01:04:01.688477800Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "(414, 500, 3)\n",
      "(414, 500)\n",
      "[[153 157 162 ... 174 173 172]\n",
      " [119 124 129 ... 173 172 171]\n",
      " [120 124 130 ... 172 171 170]\n",
      " ...\n",
      " [187 182 167 ... 202 191 170]\n",
      " [165 172 164 ... 185 141 122]\n",
      " [179 179 146 ... 197 142 141]]\n"
     ]
    }
   ],
   "source": [
    "img = cv2.imread('01_Picture/01_cat.jpg')\n",
    "print(type(img))  # img 的类型为 numpy.ndarray 类型\n",
    "print(img.shape)\n",
    "image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n",
    "print(image.shape)\n",
    "print(image)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 图像显示"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.1 图像显示(普通方法)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# opencv 默认读取格式是 BGR 格式，matplotlib 或其他库的读取格式可能是 RGB 的\n",
    "# opencv 读取并用 opencv 自带的展示函数不需要进行通道转换，但 opencv 读取后用其他库展示图片需要通道转换    \n",
    "\n",
    "# 图像显示时,可以创建多个窗口\n",
    "\n",
    "# 第一个入口参数为展示图像窗口的名字\n",
    "# 第二个入口参数为展示图像窗口中所展示的图像\n",
    "img = cv2.imread('01_Picture/01_cat.jpg')\n",
    "cv2.imshow('image_cat', img)\n",
    "\n",
    "# 等待时间，毫秒级，0表示任意键终止，5000ms表示5s\n",
    "cv2.waitKey(5000)\n",
    "\n",
    "# 销毁图像窗口\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.2 图像显示(函数方法)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-26T01:13:48.186365600Z",
     "start_time": "2024-04-26T01:13:39.869761500Z"
    }
   },
   "outputs": [],
   "source": [
    "# 绘图显示(封装函数)\n",
    "def cv_show(name, img):\n",
    "    cv2.imshow(name, img)\n",
    "    cv2.waitKey(0)\n",
    "    cv2.destroyAllWindows()\n",
    "\n",
    "\n",
    "cv_show('image_cat', img)"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.3 通道分离"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "import cv2\n",
    "\n",
    "image = cv2.imread('01_Picture/01_cat.jpg')\n",
    "b, g, r = cv2.split(image)\n",
    "cv2.imshow('image_cat 1', b)\n",
    "cv2.imshow('image_cat 2', g)\n",
    "cv2.imshow('image_cat 3', r)\n",
    "\n",
    "cv2.imshow('image_cat 4', cv2.merge([b, g, r]))\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-26T01:17:33.946335500Z",
     "start_time": "2024-04-26T01:17:29.357359500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [],
   "source": [
    "import cv2\n",
    "\n",
    "image = cv2.imread('01_Picture/01_cat.jpg')\n",
    "image[:, :, 2] = 0\n",
    "image[:, :, 1] = 0\n",
    "\n",
    "cv2.imshow('image_cat 4', image)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-26T01:20:48.694302400Z",
     "start_time": "2024-04-26T01:20:44.950707900Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3 (ipykernel)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
